package com.yuw.service.impl;

import com.yuw.bean.UserInfoBean;
import com.yuw.dao.UserInfoBeanMapper;
import com.yuw.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    // 依赖注入
    @Resource
    private UserInfoBeanMapper userInfoBeanMapper;

    @Override
    public List<UserInfoBean> selectByPrimaryKey(Integer userid) {
        return userInfoBeanMapper.selectByPrimaryKey(userid);
    }

    @Override
    public List<UserInfoBean> selectUserinfoByParams(UserInfoBean userInfoBean) {
        return userInfoBeanMapper.selectUserinfoByParams(userInfoBean);
    }

    @Override
    public boolean doLogin(UserInfoBean userInfoBean) {
        // 调用到层进行登录验证业务逻辑处理
        UserInfoBean userInfoBean1 = userInfoBeanMapper.doLogin(userInfoBean);
        System.out.println("mybatis查询结果：" + userInfoBean1);
        return userInfoBean != null;
    }

    @Override
    public int addAndUpdateUserInfo(UserInfoBean userInfoBean) {
        // 默认返回值
        int i = 0;
        // 调用dao层进行数据库操作处理
          /*
        这个方法，既可以处理新增业务逻辑，也可以处理修改业务逻辑：
        新增和修改的区别：
            新增时参数实体bean userinfobean 的主键字段userid的值 为null;
            修改时参数实体bean userinfobean 的主键字段userid的值 不为null;

         */
        if (userInfoBean.getUserid() != null) {
            // 主键字段不为null ，进行修改操作
            // 调用数据库操作dao层进行数据库保存处理
            i = userInfoBeanMapper.updateByPrimaryKeySelective(userInfoBean);
        } else {
            // 主键字段为null，则进行新增操作
            i = userInfoBeanMapper.insertSelective(userInfoBean);
        }
        return i;
    }

    @Override
    public UserInfoBean queryUserInfoByID(Integer userId) {
        return userInfoBeanMapper.queryUserInfoByID(userId);
    }

    @Override
    public int deleteByPrimaryKey(Integer userid) {
        return userInfoBeanMapper.deleteByPrimaryKey(userid);
    }

    public void setUserInfoBeanMapper(UserInfoBeanMapper userInfoBeanMapper) {
        this.userInfoBeanMapper = userInfoBeanMapper;
    }
}
